
#include		"MainMenu.hh"

MainMenu::MainMenu(sf::RenderWindow &app, IGUI::e_context &f, sf::Event& e)
  : focus(f), action(), App(app), Event(e), Input(this->App.GetInput())
{
  if (!this->bgm.LoadFromFile("sprites/bgmainmenu.jpg"))
    throw ImgException("LoadFromFile(sprites/bgmainmenu.jpg) in MaineMenu.cpp");
  if (!this->exit.LoadFromFile("sprites/exit.png"))
    throw ImgException("LoadFromFile(sprites/exit.png) in MaineMenu.cpp");
  if (!this->options.LoadFromFile("sprites/options.png"))
    throw ImgException("LoadFromFile(sprites/options.png) in MaineMenu.cpp");
  if (!this->pve.LoadFromFile("sprites/playonline.png"))
    throw ImgException("LoadFromFile(sprites/playonline.png) in MaineMenu.cpp");
  if (!this->pvp.LoadFromFile("sprites/pvp.png"))
    throw ImgException("LoadFromFile(sprites/pvp.png) in MaineMenu.cpp");
  this->action[sf::Event::KeyPressed] = &MainMenu::gere_key;
  this->action[sf::Event::MouseButtonPressed] = &MainMenu::mouse;
  this->action[sf::Event::MouseButtonReleased] = &MainMenu::mouser;
  this->action[sf::Event::Resized] = &MainMenu::resized;
  this->items.push_back(new GObject(this->exit, 3));
  this->items.push_back(new GObject(this->options, 3));
  this->items.push_back(new GObject(this->pve, 3));
  this->items.push_back(new GObject(this->pvp, 3));
  this->items.push_back(new GObject(this->bgm));
  this->items[0]->setFocus(IGUI::EXIT);
  this->items[1]->setFocus(IGUI::OPTIONMENU);
  this->items[2]->setFocus(IGUI::CONNECTMENU);
  this->items[3]->setFocus(IGUI::PVP);
  for (int i = this->items.size() - 2, inc = 200; i >= 0; i--, inc += 100) 
    this->items[i]->setPos(this->App.GetWidth() / 2 - this->items[i]->getW() / 2, inc);
  this->items[4]->resize(this->App.GetWidth(), this->App.GetHeight());
}

MainMenu::~MainMenu(void)
{
}

void		MainMenu::run(void)
{
  this->App.Clear();
  this->App.ShowMouseCursor(true);
  if (this->action.find(Event.Type) != this->action.end())
    (this->*(this->action.find(Event.Type)->second))();
  for (int i = this->items.size() - 1; i >= 0; i--) 
    this->items[i]->draw(this->App);
  this->mouseHover();
}

void		MainMenu::gere_key(void)
{
  switch (this->Event.Key.Code)
    {
    case sf::Key::Escape:
      this->focus = IGUI::EXIT;
      break;
    case sf::Key::Return:
      this->focus = IGUI::OPTIONMENU;
      break;
    case sf::Key::P:
      this->focus = IGUI::PVP;
      break;
    default:
      break;
    }
}

void		MainMenu::mouse(void)
{
  int		x = this->Input.GetMouseX();
  int		y = this->Input.GetMouseY();

  for (int i = this->items.size() - 1; i >= 0; i--)
    {
      if (y >= this->items[i]->getY() &&
	  y <= this->items[i]->getY() + this->items[i]->getH() &&
	  x >= this->items[i]->getX() &&
	  x <= this->items[i]->getX() + this->items[i]->getW())
	  this->items[i]->setState(2);
      else
	this->items[i]->setState(0);
    }
}

void		MainMenu::mouser(void)
{
  int		x = this->Input.GetMouseX();
  int		y = this->Input.GetMouseY();

  for (int i = this->items.size() - 1; i >= 0; i--)
    if (y >= this->items[i]->getY() &&
	y <= this->items[i]->getY() + this->items[i]->getH() &&
	x >= this->items[i]->getX() &&
	x <= this->items[i]->getX() + this->items[i]->getW())
      {
	this->items[i]->setState(2);
	this->focus = this->items[i]->getFocus();
      }
}

void		MainMenu::mouseHover(void)
{
  int		x = this->Input.GetMouseX();
  int		y = this->Input.GetMouseY();

  for (int i = this->items.size() - 1; i >= 0; i--)
    {
      if (y >= this->items[i]->getY() &&
	  y <= this->items[i]->getY() + this->items[i]->getH() &&
	  x >= this->items[i]->getX() &&
	  x <= this->items[i]->getX() + this->items[i]->getW())
	this->items[i]->setState(1);
      else
	this->items[i]->setState(0);
    }
}

void		MainMenu::resized(void)
{
  this->App.SetSize(1024, 768);
}
